package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserApiInfoImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //根据id查询
    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    //多个id查询
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        //匹配id
        lqw.in(UserInfo::getId,ids);
        //筛选条件
        if (userInfo != null){
            lqw.lt(userInfo.getAge() != null,UserInfo::getAge,userInfo.getAge());
            lqw.eq(StringUtils.isNotEmpty(userInfo.getGender()),UserInfo::getGender,userInfo.getGender());
            lqw.like(StringUtils.isNotBlank(userInfo.getNickname()),UserInfo::getNickname,userInfo.getNickname());
        }
        List<UserInfo> list = userInfoMapper.selectList(lqw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }
}
